package com.msj.goods.controller;

import com.github.pagehelper.PageInfo;
import com.msj.goods.common.annotation.Log;
import com.msj.goods.common.constants.JsonResultConstants;
import com.msj.goods.common.constants.UserConstants;
import com.msj.goods.common.utils.ShiroUtils;
import com.msj.goods.common.web.base.JsonResult;
import com.msj.goods.entity.GgTable;
import com.msj.goods.entity.IntegralApproval;
import com.msj.goods.entity.SysUser;
import com.msj.goods.service.GgTableService;
import com.msj.goods.service.IntegralApprovalService;
import com.msj.goods.service.IntegralLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author sun li
 * @Date 2018/11/6 11:24
 * @Description
 */
@RestController
@RequestMapping("/home")
@Api(description = "首页")
public class IndexController {

    @Autowired
    private GgTableService ggTableService;

    @Autowired
    private IntegralApprovalService integralApprovalService;

    @Autowired
    private IntegralLogService integralLogService;
        /**
         *  查询冠军首页展示
         * */
    @PostMapping(value = "/index")
    @ApiOperation(value="首页", notes="首页展示数据")
    @ApiImplicitParam(name = "",value = "json对象",required = true)
    public JsonResult home(){
        // 查询自己别人的排名
        Map<String,Object> map = new HashMap<>();
        GgTable img = ggTableService.selectById(1);
        map.put("img",img.getgImgs());
        SysUser user =  ShiroUtils.getUserEntity();
        /*integralApprovalService.selectGJ(user.getDeptId());*/
        List<Map> list = null;
        if(user.getIsApprover()== UserConstants.ADMIN){
            list =  integralApprovalService.selectAdminGJ(user.getIsApprover());
        }
        if(user.getIsApprover()==UserConstants.SUPER_ADMIN){
            list =  integralApprovalService.selectAdminAndSuperGJ();
        }
        if(user.getIsApprover()==UserConstants.COMMON){
            list =  integralApprovalService.selectGJ(user.getDeptId());
        }
        if(list.size()>0){
            for (Map mp: list) {
                if( user.getUserId().equals(mp.get("userId"))){
                    map.put("amount",mp.get("amount"));
                    map.put("rank",mp.get("rank"));
                }
            }
        }
        return JsonResult.success(map, JsonResultConstants.SUCCESS);
    }

    /** 查询第一名  1.是审批人  2.高级审批人 3.不是审批人  */
    @PostMapping(value = "/indexFirst")
    @ApiOperation(value="查询第一名", notes="查询第一名")
    @ApiImplicitParam(name = "",value = "json对象",required = true)
    public JsonResult indexFirst(){
        Map<String,Object> map = new HashMap<>();
        SysUser user =  ShiroUtils.getUserEntity();
        List<Map> list = null;

        if(user.getIsApprover()==1){
            list =  integralApprovalService.selectAdminGJ(user.getIsApprover());
        }
        if(user.getIsApprover()==2){
            list =  integralApprovalService.selectAdminAndSuperGJ();
        }
        if(user.getIsApprover()==3){
            list =  integralApprovalService.selectGJ(user.getDeptId());
        }

        if(list.size()>0 && list !=null){
            map.put("userName",list.get(0).get("userName"));
            map.put("userImg",list.get(0).get("userImg"));
        }
        return JsonResult.success(map, JsonResultConstants.SUCCESS);
    }


    @PostMapping(value = "/list")
    @Log(title = "首页审批通过的日志列表" )
    @ApiOperation(value="首页审批通过的日志列表", notes="首页审批通过的日志列表")
    @ApiImplicitParam(name = "pageSize ,pageNum ",value = "json对象",required = true)
    public JsonResult userLogin(@RequestParam("pageSize") String pageSize, @RequestParam("pageNum") String pageNum){
        PageInfo<IntegralApproval> pageInfo = integralApprovalService.selectOneIntegralApproval(pageSize,pageNum);
        return JsonResult.success(pageInfo  , JsonResultConstants.SUCCESS);
    }
}
